package com.yanqu.road.dao.db;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;

public class DbParameter {
	private static final Logger logger = LogManager.getLogger(DbParameter.class.getName());
	private final int dbType;
	private Object value;


	private int getType(Object o) {
		String name = o.getClass().getSimpleName();

        if (name.equals(Float.class.getSimpleName())) {
            return Types.FLOAT;
        }

		if (name.equals(Integer.class.getSimpleName())) {
			return Types.INTEGER;
		}

		if (name.equals(Long.class.getSimpleName())) {
			return Types.BIGINT;
		} else if (name.equals(String.class.getSimpleName())) {
			return Types.VARCHAR;
		} else if (name.equals(Byte.class.getSimpleName())) {
			return Types.INTEGER;
		} else if (name.equals(Short.class.getSimpleName())) {
			return Types.INTEGER;
		} else if (name.equals(Boolean.class.getSimpleName())) {
			return Types.BOOLEAN;
		}

		else if (name.equals(Date.class.getSimpleName())) {
			return Types.DATE;
		} else if (name.equals(Timestamp.class.getSimpleName())) {
			return Types.DATE;
		} else if (name.equals(byte[].class.getSimpleName())) {
			return Types.VARBINARY;
		}
		return 0;
	}

	private void checkType(int type, Object info) {
		// 检查类型一致性
		if (info == null) {
			return;
		}
		int tempType = type;
		if (type == Types.INTEGER || type == Types.TINYINT || type == Types.SMALLINT) {
			tempType = Types.INTEGER;
		}
		if (type == Types.TIMESTAMP || type == Types.DATE) {
			tempType = Types.DATE;
		}

		if (type == Types.FLOAT || type == Types.DECIMAL)
			tempType = Types.FLOAT;

		if (type == Types.BLOB) {
			tempType = Types.VARBINARY;
		}
		if (type == Types.NVARCHAR) {
			tempType = Types.VARCHAR;
		}
		try {
			if (tempType != getType(info)) {
				throw new Exception("type match error:" + info.getClass().getSimpleName());
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		}
	}

	public DbParameter(int type, Object value) {
		this.dbType = type;
		this.value = value;
		if (DBPoolManager.getPropertiesFile().getProperty("DataTypeCheck").equalsIgnoreCase("1"))
			checkType(type, value);
	}

	public DbParameter(int type, Date value) {
		this.dbType = type;
		this.value = new Timestamp(value.getTime());
		if (DBPoolManager.getPropertiesFile().getProperty("DataTypeCheck").equalsIgnoreCase("1"))
			checkType(type, value);
	}


    public DbParameter(int type, Object value, boolean check) {
        this.dbType = type;
        this.value = value;
        if (check && DBPoolManager.getPropertiesFile().getProperty("DataTypeCheck").equalsIgnoreCase("1"))
            checkType(type, value);
    }

	public DbParameter(int Types) {
		this.dbType = Types;
	}

	public int getDbType() {
		return dbType;
	}

	public Object getResult() {
		return value;
	}

	public void setResult(Object result) {
		this.value = result;
	}
}
